//
//  HomeTransmitView.swift
//  Weibo
//
//  Created by 郑克哥哥 on 2017/7/29.
//  Copyright © 2017年 郑克哥哥. All rights reserved.
//

import UIKit
import SnapKit
//转发微博视图
class HomeTransmitView: UIView {
    
    //记录转发微博的底部约束
    var viewBottomConstraint: Constraint?
    
    //引用viewModel
    var transmitModel:ZKStatusViewModel? {
    
        didSet{
            
            //绑定数据
            contentLabel.text = transmitModel?.retweetContent
            
            //卸载之前的底部约束
            viewBottomConstraint?.deactivate()
            
            if let picUrl = transmitModel?.status?.retweeted_status?.pic_urls, picUrl.count > 0{
                //有配图
                pictureView.isHidden = false
                
                //重新赋值转发微博底部约束
                self.snp.makeConstraints({ (make) in
                    viewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                
                //给转发微博配图绑定数据
                pictureView.picUrls = picUrl
                
            }else{
                
                //没有配图
                //隐藏配图控件
                pictureView.isHidden = true
                //重新赋值转发微博底部约束
                self.snp.makeConstraints({ (make) in
                    viewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
            }
        }
    }
    
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel(font: 14, textColor: UIColor.lightGray)
        label.numberOfLines = 0
        
        return label
    }()
    //转发微博配图
    fileprivate lazy var pictureView: HomePictureView = {
        
        let collectionView = HomePictureView()
        //注意配图颜色要一致
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(contentLabel)
        addSubview(pictureView)
    
        contentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(10)
            
            make.trailing.equalTo(self).offset(-10)
            
        }
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            make.leading.equalTo(contentLabel)
            
            
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
    
        self.snp.makeConstraints { (make) in
            viewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    
    }
    
}
